﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace DMU.Gui
{
    public partial class ucLop : DevExpress.XtraEditors.XtraUserControl
    {
        private DevExpress.XtraTab.XtraTabControl tab;
        public ucLop(DevExpress.XtraTab.XtraTabControl tab)
        {
            InitializeComponent();
            this.tab = tab;
        }

        public bool them;
        public  DataTable dtLop;
        public  DataTable dtNganh;
        public  int rowUpdate;

        Service sv = new Service();
        help hp = new help();

        private void btnThem_Click(object sender, EventArgs e)
        {
            them = true;

            GuiPro.frmThemLop frm = new GuiPro.frmThemLop(this);
            frm.ShowDialog();

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            them = false;
            int rowHandle = gridView1.FocusedRowHandle;
            if (rowHandle >= 0)
            {
                GuiPro.frmThemLop frm = new GuiPro.frmThemLop(this);
                if (gridView1.GetRowCellValue(rowHandle, "MaLop") != null)
                {
                    
                    frm.txtMa.Text = gridView1.GetRowCellValue(rowHandle, "MaLop").ToString();
                    frm.txtTen.Text = gridView1.GetRowCellValue(rowHandle, "TenLop").ToString();
                    frm.txtMaGV.Text = gridView1.GetRowCellValue(rowHandle, "GVCN").ToString();
                    frm.txtGhiChu.Text = gridView1.GetRowCellValue(rowHandle, "GhiChu").ToString();
                    frm.lkpDT.Text = gridView1.GetRowCellValue(rowHandle, "MaLoaiDT").ToString();
                    frm.txtMaNganh.Text = gridView1.GetRowCellValue(rowHandle, "MaNganh").ToString();
                    frm.chkActive.Checked = Convert.ToBoolean(gridView1.GetRowCellValue(rowHandle, "IsActive").ToString());
                }
                frm.Text = "Bảng cập nhật thông tin lớp";
                frm.Size = new System.Drawing.Size(540, 340);
                frm.txtMa.Properties.ReadOnly = true;
                frm.ShowDialog();
            }
            else
                hp.showMes("Chọn dòng để thực hiện chức năng này !", -1);
        }

        private void ucLop_Load(object sender, EventArgs e)
        {
            dtNganh = sv.getDatatable("dmu_proPublicDTLoad", null);
            repositoryItemGridLookUpEdit1.DataSource = dtNganh;
            repositoryItemGridLookUpEdit1.DisplayMember = "LoaiDT";
            repositoryItemGridLookUpEdit1.ValueMember = "MaLoaiDT";

            #region
            dtLop = new DataTable();

            DataColumn[] cl = new DataColumn[7];
            cl[0] = new DataColumn("MaLop", Type.GetType("System.String"));
            cl[1] = new DataColumn("TenLop", Type.GetType("System.String"));
            cl[2] = new DataColumn("GVCN", Type.GetType("System.Int32"));
            cl[3] = new DataColumn("GhiChu", Type.GetType("System.String"));
            cl[4] = new DataColumn("MaLoaiDT", Type.GetType("System.String"));
            cl[5] = new DataColumn("MaNganh", Type.GetType("System.String"));
            cl[6] = new DataColumn("IsActive", Type.GetType("System.Boolean"));

            dtLop.Columns.AddRange(cl);
            gridControl1.DataSource = dtLop;
            #endregion
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            tab.TabPages.RemoveAt(tab.SelectedTabPageIndex);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int[] rowSelec = gridView1.GetSelectedRows();
            int dem=0; int len = rowSelec.Length;
            
            for (int i = 0; i < len; i++)
            {
                if (rowSelec[i] < 0)
                {
                    gridView1.UnselectRow(rowSelec[i]);
                }
                else
                    break;
            }

            rowSelec = gridView1.GetSelectedRows(); len = rowSelec.Length;

            if (len > 0)
            {
                try
                {
                    if (hp.showMes(len + " đối tượng sẽ bị xóa ?", 2) == DialogResult.Yes)
                    {
                        sv.openConnect();
                        for (int i = 0; i < len; i++)
                        {
                            
                            if (gridView1.GetRowCellValue(rowSelec[i],"MaLop") != null)
                            {
                                string s = sv.cusTomExecuteScalarStore("dmu_proLXoaLop", new SqlParameter("MaLop0", gridView1.GetRowCellValue(rowSelec[i],"MaLop").ToString()));
                                
                                if (s != "1")
                                {
                                    gridView1.UnselectRow(rowSelec[i]);
                                    dem++;
                                }
                            }
                        }

                        sv.closeConnect();
                        gridView1.DeleteSelectedRows();
                        if (dem > 0)
                            hp.showMes(dem + " đối tượng không thể xóa !", 1);
                        else
                            hp.showMes("Xóa thành công "+len +" đối tượng", -1);
                    }
                }
                catch (Exception et)
                {
                    hp.showMes("Có lỗi khi xóa. Vui lòng thử lại ! " + et.Message, 1);
                }
            }
            else
            {
                hp.showMes("Chọn dòng để thực hiện chức năng này !!", -1);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
           
            if (txtFind.Text.Trim() == "")
            {
                txtFind.Focus(); return;
            }
            dtLop = sv.getDatatable("dmu_proLFindToTen", new SqlParameter("TenLop0", txtFind.Text + "%"));
            gridControl1.DataSource = dtLop;
        }

        private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }

        private void txtFind_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                EventArgs et = new EventArgs();
                btnFind_Click(sender, et);
            }

        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                EventArgs et = new EventArgs();
                btnXoa_Click(sender, et);
            }
        }

        private void gridView1_RowCellClick_1(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

        private void txtFind_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            dtLop = sv.getDatatable("dmu_proLLoadLop", null);
            gridControl1.DataSource = dtLop;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }



    }
}
